<template>
	<view class="">

	</view>
</template>

<script setup>
import {
	onLoad,
	onUnload,
	onReady
} from '@dcloudio/uni-app';
import {
	taskAd,
	taskreceive
} from "@/api/api";
import {
	ref
} from "vue";
import {
	uiwu
} from '@/config/config';
const adInfo = ref(null);

const initAd = (id) => {
	// #ifdef MP-TOUTIAO
	taskAd().then(({
		data: {
			adUnitId,
			id
		}
	}) => {
		adInfo.value = tt.createRewardedVideoAd({
			adUnitId
		})
		adInfo.value.onError((err) => {
			console.log(err);
			uiwu.hideloading()
			switch (err.errCode) {
				case 1004:
					uiwu.toast('暂无广告')
					break;
				default:
					uiwu.toast('更多请参考错误码文档')
			}
		})
		adInfo.value.onClose(async (res) => {
			console.log(res);
			uiwu.hideloading()
			if (res.isEnded) {
				const result = await taskreceive({
					user_task_id: id,
					task_form: 4
				})
				if (!result) return
				uiwu.toast(result.msg)
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			} else {
				uiwu.toast('中途退出不能获取奖励')
			}
		});
		adInfo.value.load()


		adInfo.value.show()
	})
	// #endif
}

onLoad((op) => {
	uiwu.loading('加载中...', true)
	initAd(op.id)
})
</script>

<style scoped lang="scss"></style>